require("AWZ")
require("tsp")


app = {}
app.bid = 'com.kysec.iphone'
app.name = '开源证券肥猫'
app.other = '留存'
t = {}
degree = 80
t['开屏跳过'] = { 0xffffff, "0|0|0xffffff,25|-1|0xff4548,-32|4|0xffffff,-361|150|0xffe7a9,-548|75|0xff6364", degree, 49, 61, 728, 415 }
t['同意并继续'] = { 0xfe5354, "0|0|0xfe5354,3|-9|0xffffff,-277|0|0xfe484a,-408|-30|0xffffff", degree, 87, 898, 664, 1023 }
t['越狱提示'] = { 0xfe484a, "0|0|0xfe484a,17|-10|0xffffff,-301|1|0xfe484a,-86|-202|0x333333", degree, 105, 512, 633, 808 }
t['新股'] = { 0x333333, "0|0|0x333333,-9|-22|0xfe484a,-19|-21|0xfe484a", degree, 33, 450, 738, 602 }
t['_vpn好'] = { 0x007aff, "0|0|0x007aff,-18|-8|0x007aff", degree, 333, 644, 412, 819 }
t['开源_立即体验'] = { 0xff3b14, "0|0|0xff3b14,-123|-2|0xfffffe", degree, 165, 1101, 583, 1244 }
t['开源_同意'] = { 0xff3d3d, "0|0|0xff3d3d,-315|-1|0x909090", degree, 87, 931, 614, 1005 }

function idfa__()
    timeline = os.time()
    while ( os.time() - timeline < rd(18,24) ) do
        if active(app.bid, 5) then
            randomKey = true
            
            tips = {"开屏跳过","同意并继续","越狱提示","新股", "_vpn好","开源_立即体验","开源_同意"}
            for k,v in ipairs(tips) do
                if d(v,true) then
                    randomKey = false
                    break
                end
            end
            
            if randomKey then
                moveTo(600,300,100,100,30,50)
				delay(1)
				click( rd(100,500), rd(100,900))
				click(453, 954)
				delay(1)
            end
            
            
        end
    end

end


function awz_()
	local logTxt = '/var/mobile/iggresult.txt'
	out_time = os.time()
	while os.time()-out_time <= 10 do
		if fileExists(logTxt)then
			local new = readFile(logTxt)[1]
			log(new)
			if new == "1" then
				return true
			elseif new == "3" then
				toast('IP-->重复请注意',1)
				return true
			elseif new == '2' then
				toast('一键新机中',2)
			end
			mSleep(1000)
		end
		mSleep(1000* 1)
	end
end


t['awz_报错'] = { 0xff0606, "0|0|0xff0606,273|-18|0xff7f00,546|-46|0x0950d0", degree, 10, 1234, 740, 1326 }
t['awz_一键新机'] = { 0x6f7179, "0|0|0x6f7179,105|-88|0x34c759,534|-93|0x34c759,361|-6|0x6f7179", 90, 21, 488, 740, 865 }
t['awz_一键新机'] = { 0x6f7179, "0|0|0x6f7179,306|0|0x6f7179,36|101|0x6f7179,286|95|0x6f7179,345|657|0xff7f00,372|638|0x0950d0", degree, 11, 569, 738, 1325 }
t['awz_返回'] = { 0x007aff, "0|0|0x007aff,70|-4|0x007aff,73|-5|0xf9f9f9,77|0|0x007aff", 90, 5, 44, 732, 114 }
t['awz_正在新机'] = { 0x34c759, "0|0|0x34c759,431|-3|0x34c759,-55|38|0xd2d3d5,369|40|0xd4d5d7,367|221|0xd4d4d6,-56|224|0xd1d1d4", degree, 32, 496, 736, 780 }
t['awz_正在新机'] = { 0xd5d5d7, "0|0|0xd5d5d7,414|3|0xd5d5d7,434|9|0xffffff,422|188|0xd4d4d6,-1|190|0xd3d3d6", degree, 126, 554, 622, 778 }

function awzNew_()
    timeLine = os.time()
	outTime = 60 * 0.5
	while (os.time()-timeLine < outTime) do
		if active("AWZ",3)then
		    if d("awz_报错") then
		        closeApp("AWZ")
		        delay(2)
            elseif d("awz_一键新机", true ) then
                local check = 0
                local todo = false
                while (check < 30 and d("awz_正在新机") ) do
                    delay(1)
                    check = check + 1
                    log("awz_正在新机")
                end
                return true
            elseif d("awz_返回", true) then
            end
		end
		d("_vpn好", true)
		mSleep(1000)
	end
	nLog('新机超时')
end

function up()
	local url = 'http://wenfree.cn/api/Public/idfa/?service=idfa.idfa'
	local idfalist ={}
	idfalist.name = app.name
	idfalist.phonename = getDeviceName()
	idfalist.idfa = app.idfa
	idfalist.account = app.account
	idfalist.other = app.other
	idfalist.device = app.device
	return post(url,idfalist)
end

function get_TOKEN_IDFA()
    local path = appDataPath(app.bid).."Documents/tztKeyChainFile"
    log(path)
    tzKeyChainFile = readFile(path)
    log(tzKeyChainFile)
    
    idfa = tzKeyChainFile[6]
    idfa = string.gsub(idfa, "<string>", "")
    app.idfa = string.gsub(idfa, "</string>", "")
    log( idfa  )
    
    idfa = tzKeyChainFile[10]
    idfa = string.gsub(idfa, "<string>", "")
    app.account = string.gsub(idfa, "</string>", "")
    
    token = tzKeyChainFile[12]
    token = string.gsub(token, "<string>", "")
    app.device = string.gsub(token, "</string>", "")
    log( token )
    return
end

function main()
    i = 0
    
    while ( i < 1000 ) do
        log('准备一键新机')
        d("_vpn好", true)
        if awzNew_() then
            
            -- if vpn() then
            idfa__()
            get_TOKEN_IDFA()
            up()
            -- end
        end
        
        vpnx()
        delay(2)
        setVPNEnable(true)
        -- reName('e'.. os.date('%c') )
        -- closeApp("AWZ")   
        delay(1)
        log('awz,重命名')
        i=i+1
    end
    
end



while (true) do
	local ret,errMessage = pcall(main)
	if ret then
	else
	    vpnx();
		log(errMessage)
		dialog(errMessage, 10)
		mSleep(2000)
	end
end










































